package com.util;

import javax.jws.soap.SOAPBinding;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static String DRIVER;
    private static String URL;
    private static String USERNAME;
    private static String PASSWORD;
    private static Connection conn =null;
    private static PreparedStatement ps=null;
    private static ResultSet rs=null;
    static{
//静态代码块
        try {
            //创建对象
            Properties properties = new Properties();
            //读取配置文件
            InputStream is =
                    JDBCUtil.class.getClassLoader().getResourceAsStream("mysql.properties");
            properties.load(is);
            //赋值
            DRIVER=properties.getProperty("driver");
            URL=properties.getProperty("url");
            USERNAME=properties.getProperty("username");
            PASSWORD=properties.getProperty("password");
            Class.forName(DRIVER);//加载
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        return conn;
    }
    //关闭
    public static void close() throws SQLException {
        rs.close();
        ps.close();
        conn.close();
    }

    //注册 insert into user(name,username,password,age) values(?,?,?,?);
    public static int redis(String sql,String ...str ) throws SQLException {
        //调用connection创建执行器
        ps = getConnection().prepareStatement(sql);
        //用for循环传递参数
        for (int i = 0; i <str.length ; i++) {
            ps.setString(i+1,str[i]);
        }
        //执行获取数据
        int i = ps.executeUpdate();
        return i;
    }

    //登录 select * from user where username=? and password=?
    public static ResultSet login(String sql,String username,String password) throws SQLException {
        //通过调用connection创建执行器对象
        ps = getConnection().prepareStatement(sql);
        //传递参数
        ps.setString(1,username);
        ps.setString(2,password);
        //获取结果集
        rs = ps.executeQuery();
        return rs;
    }
//修改

//查询
//删除
}